{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python压缩(>=3.7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import zipfile\n",
    "import os\n",
    "\n",
    "DATADIR = 'datadir'\n",
    "\n",
    "if not os.path.exists(DATADIR):\n",
    "    os.makedirs(DATADIR)\n",
    "\n",
    "for i in range(10):\n",
    "    with open(f'{DATADIR}/{i:02d}', 'w', encoding='utf-8') as f:\n",
    "        for i in range(500000):\n",
    "            f.write('{}\\n'.format(i))\n",
    "\n",
    "algs = (\n",
    "    ('STORED', zipfile.ZIP_STORED),\n",
    "    ('DEFLATED', zipfile.ZIP_DEFLATED),\n",
    "    ('BZIP2', zipfile.ZIP_BZIP2),\n",
    "    ('LZMA', zipfile.ZIP_LZMA),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RAW      :  3.71MB\n",
      "STORED   : 37.09MB\n",
      "DEFLATED : 10.10MB\n",
      "BZIP2    :  5.68MB\n",
      "LZMA     :  0.85MB\n"
     ]
    }
   ],
   "source": [
    "print('{:9}: {:5.2f}MB'.format('RAW', os.path.getsize(f'{DATADIR}/00') / 1024 ** 2))\n",
    "\n",
    "for name, alg in algs:\n",
    "    zp_filename = f'{DATADIR}/{name}.zip'\n",
    "    zp = zipfile.ZipFile(zp_filename, 'w', compression=alg)\n",
    "    for i in range(10):\n",
    "        zp.write(f'{DATADIR}/{i:02d}', f'{i:02d}')\n",
    "    print('{:9}: {:5.2f}MB'.format(name, os.path.getsize(zp_filename) / 1024 ** 2))"
   ]
  }
 ],
 "metadata": {
  "file_extension": ".py",
  "kernelspec": {
   "display_name": "Python 3.7.4 64-bit ('py37': conda)",
   "language": "python",
   "name": "python37464bitpy37conda124ef028e81f4ec7808674584596f802"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  },
  "version": 3
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
